<?php declare(strict_types=1);
/**
 * This file is part of Swoft.
 *
 * @link     https://swoft.org
 * @document https://swoft.org/docs
 * @contact  group@swoft.org
 * @license  https://github.com/swoft-cloud/swoft/blob/master/LICENSE
 */

namespace App\Http\Controller\Test;

use App\Library\Core\Api;
use Swoft\Http\Message\ContentType;
use Swoft\Http\Message\Request;
use Swoft\Http\Message\Response;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Http\Server\Annotation\Mapping\RequestMethod;
use Throwable;
use function context;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
use App\Exception\ApiException;
use App\Library\Core\ErrCode;

/**
 * Class HomeController
 * @Controller()
 */
class HomeController extends BaseController
{

    /**
     * @RequestMapping("/home/index",method={RequestMethod::GET})
     * @throws Throwable
     */
    public function index()
    {
        /*
            array(5) {
              ["appId"]=> string(10) "service-gateway"
              ["cluster"]=> string(7) "default"
              ["namespaceName"]=> string(11) "application"
              ["configurations"]=> array(4) {
                ["consul"]=> string(101) "{"SERVICE_ID":"service-gateway","SERVICE_NAME":"service-gateway","HOST":"172.25.0.10","PORT":8500,"TIMEOUT":10}"
                ["application"]=> string(18) "{"HTTP_PORT":8001}"
                ["redis"]=> string(47) "{"HOST":"172.25.0.3","PORT":6379,"PASSWORD":""}"
                ["db"]=> string(208) "{"DB_W_DSN":"mysql:dbname=sw_framework;host=172.25.0.4","DB_W_USERNAME":"bingo","DB_W_PASSWORD":"bingo","DB_R_DSN":"mysql:dbname=sw_framework;host=172.25.0.10","DB_R_USERNAME":"bingo","DB_R_PASSWORD":"bingo"}"
              }
              ["releaseKey"]=> string(31) "20210520141018-74a94754f9cc2fc8"
            }
         */
        $data = $this->config->pull('application');
        return $data['configurations'];
    }

    /**
     * @RequestMapping("/home/test")
     * @return array
     */
    public function test(Request $request)
    {
        $k = $request->input('k');
        return ['res'=>config($k)];
    }

    /**
     * 网关调用子服务路径[校验header->x-Authorization->token]
     * @RequestMapping("/call/{service}/{path}")
     *
     * @return Response
     */
    public function call(string $service , string $path)
    {
        $request = context()->getRequest();
        $params = array_merge($request->getQueryParams() , $request->getPost());
        $params['userInfo'] = context()->get('userInfo');
        $api = new Api();

        /*
            $service == 'service-gateway' &&
            $path == 'Fastdfs.upload' &&
            context()->getRequest()->getMethod() == 'POST' &&
            !empty($request->file('image')
         */

        if(!empty($request->file('image'))){
            if(!($service == 'service-gateway' && $path == 'Fastdfs.upload')){
                throw new ApiException(ErrCode::msg(ErrCode::ERR_UPLOAD_PATH) ,ErrCode::ERR_UPLOAD_PATH);
            }
            if(context()->getRequest()->getMethod() != 'POST'){
                throw new ApiException(ErrCode::msg(ErrCode::ERR_HTTP_METHOD) ,ErrCode::ERR_HTTP_METHOD);
            }
            $flag = $this->uploadFile($request->file('image'));
            if($flag){
                $params['image'] = $this->uploadFile($request->file('image'));
            }else{
                throw new ApiException(ErrCode::msg(ErrCode::UPLOAD_FASTDFS_ERROR) ,ErrCode::UPLOAD_FASTDFS_ERROR);
            }
        }
        $res = $api->call($service , $path , $params,context()->getRequest()->getMethod());
        return $res;
    }

    /**
     * 调用网关内的服务[不校验header->x-Authorization->token]
     * @RequestMapping("/innerCall/{service}/{path}")
     *
     * @return Response
     */
    public function innerCall(string $service , string $path)
    {
        $request = context()->getRequest();
        $params = array_merge($request->getQueryParams() , $request->getPost());
        $api = new Api();
        $res = $api->call($service , $path , $params,context()->getRequest()->getMethod());
        return $res;
    }
}
